package net.xdclass.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.EventMessageType;
import net.xdclass.exception.BizException;
import net.xdclass.model.EventMessage;
import net.xdclass.service.ShortLinkService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@RabbitListener(queuesToDeclare = {@Queue("short_link.update.mapping.queue")})
public class ShortLinkUpdateMappingMQListener {
	@Autowired
	private ShortLinkService shortLinkService;

	@RabbitHandler
	public void shortLinkHandler(EventMessage eventMessage, Message message, Channel channel) throws IOException {
		log.info("监听到消息ShortLinkUpdateMappingMQListener message消息内容:{}", message);
		try {
			eventMessage.setEventMessageType(EventMessageType.SHORT_LINK_UPDATE_MAPPING.name());
			shortLinkService.handlerUpdateShortLink(eventMessage);
		} catch (Exception e) {
			log.error("消费消息失败:{}", eventMessage);
			throw new BizException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
		}
		log.info("消费成功:{}", eventMessage);
		//确认消息消费成功
		//channel.basicAck(tag, false);
	}
}

